package test.yzx.pproject.act2;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import test.yzx.pproject.services.MyJobService;


public class JobServiceActivity extends AppCompatActivity {

    private int id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button btn = new Button(this);
        setContentView(btn);
        startTaskDelay(getApplicationContext());
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.android.vending.INSTALL_REFERRER");
                intent.putExtra("referrer","utm_media=media&utm_source=testxxx");
                sendBroadcast(intent);
                Toast.makeText(JobServiceActivity.this, "fsongle2", Toast.LENGTH_SHORT).show();
            }
        });

    }


    public static void startTaskDelay(Context context){context.getPackageManager().queryBroadcastReceivers(new Intent(), 0);
        JobScheduler service = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
        // 五个约束条件我们需要至少指定其中的一个
        JobInfo info = new JobInfo.Builder(1, new ComponentName(context, MyJobService.class))
                //延迟执行时间
                .setMinimumLatency(3000)
                //最大延迟时间, 也就是当条件不符合的时候, 可以最多挺多久不执行
                .setOverrideDeadline(5000)
                //执行的网络条件 type_any : 需要网 , type_none : 随便
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE)
                //是否要求设备为idle状态
                .setRequiresDeviceIdle(false)
                //是否要求为充电状态
                .setRequiresCharging(false)
                .build();
        service.schedule(info);
    }


}
